package com.sctvcloud.qingchuan.ui.dialog;

import android.content.DialogInterface;
import android.view.View;
import android.widget.RatingBar;

import com.ruihang.generalibrary.ui.dialog.BaseBottomSheetFrag;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.beans.FUsers;
import com.sctvcloud.qingchuan.http.AbsNetCallBack;
import com.sctvcloud.qingchuan.http.NetUtils;
import com.sctvcloud.qingchuan.ui.utils.KeyBoardUtils;
import com.sctvcloud.qingchuan.utils.UserManager;

public class PoliticsEvaluateDiaFragment extends BaseBottomSheetFrag implements View.OnClickListener {
    private RatingBar politics_score;
    private CustomFontTextView dia_cancel;
    private CustomFontTextView dia_confirm;
    private SendScoreListener sendScoreListener;

    private long interactionId;
    private float politicsRating = 0;

    private int types;

    public int getTypes() {
        return types;
    }

    public void setTypes(int types) {
        this.types = types;
    }


    @Override
    public int getLayoutResId() {
        return R.layout.dia_politics_evaluate_fragment;
    }

    public void setSendScoreListener(SendScoreListener sendScoreListener) {
        this.sendScoreListener = sendScoreListener;
    }


    @Override
    public void initView() {
        politics_score = (RatingBar) rootView.findViewById(R.id.politics_score);
        dia_cancel = (CustomFontTextView) rootView.findViewById(R.id.dia_cancel);
        dia_confirm = (CustomFontTextView) rootView.findViewById(R.id.dia_confirm);
        dia_cancel.setOnClickListener(this);
        dia_confirm.setOnClickListener(this);
        politics_score.setRating(5);
        politicsRating = 5;
        politics_score.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                politicsRating = rating;
            }
        });
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View v) {
        if (v == dia_confirm) {

            evaluatePolitics();
            KeyBoardUtils.closeKeyboard(getActivity());
        } else if (v == dia_cancel) {
            dismissAllowingStateLoss();
        }
    }

    private int getStrLength(String str) {
        if (str == null) {
            return 0;
        }
        return str.length();
    }

    public interface SendScoreListener {
        void onEvaluateSendSuccess();
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);

    }

    /**
     * 发送问政追问
     *
     * @return
     */
    private void evaluatePolitics() {
        if (politicsRating == 0) {
            toast("评分需大于1星");
            return;
        }
        if (interactionId > 0) {
            FUsers user = UserManager.getInstance().getUser();
            if (null != UserManager.getInstance().getUser()) {
                String userId = user.getUserId();
                NetUtils.getNetAdapter().toSendPoliticsEvaluate(getOwnerName(), Constances.TENANT_ID, interactionId, (int) politicsRating, user.getNickName(), userId, new AbsNetCallBack<String>(String.class) {
                    @Override
                    public void onSucStr(String result) {
                        if (null != sendScoreListener) {
                            sendScoreListener.onEvaluateSendSuccess();
                        }
                        closeProgress();
                        dismissAllowingStateLoss();
                    }

                    @Override
                    public void onError(Throwable ex, String errorMsg) {
                        toast(errorMsg);
                    }


                    @Override
                    public void onEnd() {
                        closeProgress();
                    }
                });
            }
        } else {
            toast("选择回复错误");
        }
    }

    /**
     * 传入问政回复相关参数
     *
     * @param interactionId
     */
    public void setPoliticsInfo(long interactionId) {
        this.interactionId = interactionId;
    }
}
